package org.example.memora.configur;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.example.memora.store.DualChatMemoryStore;
import org.example.memora.store.MongoChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class XiaoyouAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Autowired
    private DualChatMemoryStore dualStore;

    @Autowired
    private EmbeddingModel embeddingModel;

    @Autowired
    private EmbeddingStore<TextSegment> embeddingStore;
    @Bean(name = "qwenChatModel")
    public ChatLanguageModel qwenChatModel() {
        return QwenChatModel.builder()
                .apiKey("sk-edc59143ef3b4230b2c767a295d409f3")
                .modelName("qwen-max")
                // 可换成你账号支持的模型
                .build();
    }

//    @Bean
//    public ChatMemoryProvider chatMemoryProviderXiaozhi() {
//        return memoryId -> {
//            MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
//                    .id(memoryId)
//                    .maxMessages(20)
//                    .chatMemoryStore(mongoChatMemoryStore)
//                    .build();
//            return memory;
//        };
//    }
    @Bean("chatMemoryProviderXiaoyou")
    public ChatMemoryProvider chatMemoryProviderXiaoyou( ){
        return memoryId -> {
            MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
                    .id(memoryId)
                    .maxMessages(20)
                    .chatMemoryStore(dualStore)
                    .build();
            return memory;
        };
    }



    //基于内存向量存储的检索器
//    @Bean
//    public ContentRetriever contentRetrieverXiaozhi() {
//
//        Document document1 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/医院信息.md");
//        Document document2 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/科室信息.md");
//        Document document3 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/神经内科.md");
//        List<Document> documents = Arrays.asList(document1, document2, document3);
//
//        //使用内存向量存储
//        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
//        //使用默认的文档分割器
//        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
//        //从嵌入存储（EmbeddingStore）里检索和查询内容相关的信息
//        return EmbeddingStoreContentRetriever.from(embeddingStore);
//
//    }

    //基于Pinecone向量存储的检索器
    @Bean
    public ContentRetriever contentRetrieverPinecone(){

        return EmbeddingStoreContentRetriever
                .builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .maxResults(10)
                .minScore(0.5)
                .build();
    }
    @Bean(name = "chatMemory")
    public ChatMemory chatMemory() {
        return MessageWindowChatMemory.builder()
                .id("default")
                // 单一会话 ID，可任意设置
                .maxMessages(10)
                .chatMemoryStore(new InMemoryChatMemoryStore())
                //  替换为内存存储
                .build();
    }
}

